/**
 * 
 */
package com.tomagency.cyberlab.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.tomagency.cyberlab.client.rpc.ScannerServiceAsync;

import de.gammadata.gwt.modelle.ResultObject;

/**
 * @author gfr
 *
 */
public class ScannerApp extends Composite {

	private static ScannerAppUiBinder uiBinder = GWT
			.create(ScannerAppUiBinder.class);

	interface ScannerAppUiBinder extends UiBinder<Widget, ScannerApp> {
	}

	/**
	 * Because this class has a default constructor, it can
	 * be used as a binder template. In other words, it can be used in other
	 * *.ui.xml files as follows:
	 * <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 *   xmlns:g="urn:import:**user's package**">
	 *  <g:**UserClassName**>Hello!</g:**UserClassName>
	 * </ui:UiBinder>
	 * Note that depending on the widget that is used, it may be necessary to
	 * implement HasHTML instead of HasText.
	 */
	public ScannerApp() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	RpcServiceProvider serviceProvider;



	@UiField
	Button statusbutton;
	@UiField
	Button scanbutton;
	@UiField
	Button startbutton;
	@UiField
	Button stopbutton;
	@UiField
	Label message;

	public ScannerApp(String buttonLabel,RpcServiceProvider serviceProvider) {
		this.serviceProvider=serviceProvider;
		initWidget(uiBinder.createAndBindUi(this));

		// Can access @UiField after calling createAndBindUi
		statusbutton.setText(buttonLabel);
		scanbutton.setText("Rescan");
		startbutton.setText("Start");
		stopbutton.setText("Stop");
		
	}
	
	public void init(){
		fetchStatus();
	}
	


	@UiHandler("statusbutton")
	void onClickStatus(ClickEvent e) {
		fetchStatus();
	}

	public void setButtonText(String text) {
		statusbutton.setText(text);
	}
	
	@UiHandler("scanbutton")
	void onClickScan(ClickEvent e) {
		message.setText("Fordere Scan am Server an");
		serviceProvider.getScannerService().doImmediateScan(new AsyncCallback<ResultObject>(){

			@Override
			public void onFailure(Throwable caught) {
				message.setText("Fehler "+caught.getMessage());
				
			}

			@Override
			public void onSuccess(ResultObject result) {
				message.setText(result.getMessage());
				
			}
			
		});
	}
	
	@UiHandler("startbutton")
	void onClickStart(ClickEvent e) {
		doStartStop(true);
	}
	@UiHandler("stopbutton")
	void onClickStop(ClickEvent e) {
		doStartStop(false);
	}

	private void fetchStatus(){
		message.setText("hole Status vom Server");
		serviceProvider.getScannerService().getTaopixScannerStatus(new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				message.setText("Fehler "+caught.getMessage());
				
			}

			@Override
			public void onSuccess(String result) {
				message.setText(result);
				
			}
			
		});
	}
	
	private void doStartStop(boolean start){
		if (start)
			message.setText("Fordere Start-Scan am Server an");
		else
			message.setText("Fordere Stopp-Scan am Server an");
		
		serviceProvider.getScannerService().changeSannerRunModus(start,new AsyncCallback<ResultObject>(){

			@Override
			public void onFailure(Throwable caught) {
				message.setText("Fehler "+caught.getMessage());
				
			}

			@Override
			public void onSuccess(ResultObject result) {
				message.setText(result.getMessage());
				
			}
			
		});
		
	}


	/**
	 * Gets invoked when the default constructor is called
	 * and a string is provided in the ui.xml file.
	 */
	public String getButtonText() {
		return statusbutton.getText();
	}


}
